<template>
  <div class="app-container">
    <el-form :inline="true" class="filter-container">
      <el-form-item prop="surveys">
        <el-select v-model="surveysId" size="small" placeholder="请选择问卷" filterable clearable>
          <el-option v-for="item in surveysOptions" :key="item.id" :label="item.headline" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="addMeetingSurveys"
        >添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column label="ID" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="问卷标题">
        <template slot-scope="scope">
          {{ scope.row.headline }}
        </template>
      </el-table-column>
      <el-table-column label="类型" width="110" align="center">
        <template slot-scope="scope">
          <el-tag
            size="small"
            effect="plain"
          >
            {{ scope.row.typeName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上线" width="110" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.online"
            :active-value="1"
            :inactive-value="0"
            @change="switchSurveysOnline(scope.row.id, scope.row.online)"
          />
        </template>
      </el-table-column>
      <el-table-column label="可重复答题" width="110" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.repeat"
            :active-value="1"
            :inactive-value="0"
            @change="switchSurveysRepeat(scope.row.id, scope.row.repeat)"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220" align="center">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="success"
            icon="el-icon-download"
            plain
            :loading="exportListLoading"
            :disabled="exportListLoading"
            @click="exportList(scope.row.id)"
          >调研记录</el-button>
          <el-dropdown trigger="click">
            <el-button
              size="small"
              plain
            >操作 <i class="el-icon-arrow-down el-icon--right" /></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="toSurveysEdit(scope.row.id)"
              >编辑问卷</el-dropdown-item>
              <el-dropdown-item
                @click.native="toSurveysQuestions(scope.row.id)"
              >设置题目</el-dropdown-item>
              <el-dropdown-item
                divided
                @click.native="pushSurvey(scope.row.id)"
              >调研推送</el-dropdown-item>
              <!-- <el-dropdown-item
                @click.native="toSurveysResult(scope.row.id)"
              >调研结果</el-dropdown-item> -->
              <el-dropdown-item
                divided
                @click.native="removeMeetingSurveys(scope.row.id)"
              >
                <span class="text-danger">移除</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :auto-scroll="false" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="fetchData" />
  </div>
</template>

<script>
import {
  getMeetingSurveys,
  getAllSurveys,
  pushMeetingSurveys,
  addMeetingSurveys,
  removeMeetingSurveys,
  updateMeetingSurveysOnline,
  updateMeetingSurveysRepeat,
  getMeetingSurveysLog
} from '@/api/surveys'
import Pagination from '@/components/Pagination'
import { blobExport } from '@/utils'

export default {
  name: 'MeetingSurveysList',
  components: {
    Pagination
  },
  props: {
    meetingId: {
      type: String,
      default: ''
    },
    meetingName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        meetingId: undefined,
        type: 0,
        page: 1,
        size: 20
      },
      surveysId: '',
      surveysOptions: [],
      exportListLoading: false
    }
  },
  created() {
    this.listQuery.meetingId = this.meetingId
    this.fetchAllSurveys()
    this.fetchData()
  },
  methods: {
    fetchAllSurveys() {
      getAllSurveys({
        meetingId: this.meetingId,
        type: 0
      }).then(response => {
        // console.log(response)
        this.surveysOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData() {
      this.listLoading = true
      getMeetingSurveys(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    addMeetingSurveys() {
      if (this.surveysId === '') {
        this.$message({
          type: 'warning',
          message: '请选择问卷！'
        })
      } else {
        addMeetingSurveys({
          meetingId: this.meetingId,
          surveysId: this.surveysId,
          repeat: 1
        }).then(response => {
          // console.log(response)
          this.surveysId = ''
          this.fetchAllSurveys()
          this.fetchData()
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }
    },
    pushSurvey(id) {
      this.$confirm('确认发送调研推送?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        pushMeetingSurveys({
          meetingId: this.meetingId,
          surveysId: id
        }).then(response => {
          // console.log(response)
          if (response.rtncode === 200) {
            this.$message({
              type: 'success',
              message: '发送成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: '操作失败，请稍后再试!'
            })
          }
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消发送'
        })
      })
    },
    toSurveysEdit(id) {
      this.$router.push(`/surveys/edit/${id}`)
    },
    toSurveysQuestions(id) {
      this.$router.push(`/surveys/list/${id}/questions`)
    },
    switchSurveysOnline(id, online) {
      updateMeetingSurveysOnline({
        meetingId: this.meetingId,
        surveysId: id,
        online
      }).then(response => {
        // console.log(response)
        this.$message({
          type: 'success',
          message: '设置成功!'
        })
      }).catch(err => {
        console.log(err)
      })
    },
    switchSurveysRepeat(id, repeat) {
      updateMeetingSurveysRepeat({
        meetingId: this.meetingId,
        surveysId: id,
        repeat
      }).then(response => {
        // console.log(response)
        this.$message({
          type: 'success',
          message: '设置成功!'
        })
      }).catch(err => {
        console.log(err)
      })
    },
    toSurveysResult(id) {
      console.log(id)
    },
    exportList(id) {
      this.exportListLoading = true
      getMeetingSurveysLog({
        meetingId: this.meetingId,
        surveysId: id,
        type: 0
      }).then(response => {
        // console.log(response)
        blobExport(response, `${this.meetingName} - 会后调研记录.xlsx`)

        this.$message({
          type: 'success',
          message: '导出成功！',
          duration: 5000
        })
        this.exportListLoading = false
      }).catch(err => {
        console.log(err)
        this.exportListLoading = false
      })
    },
    removeMeetingSurveys(id) {
      this.$confirm('确认移除该问卷?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeMeetingSurveys({
          meetingId: this.meetingId,
          surveysId: id
        }).then(response => {
          // console.log(response)
          this.fetchAllSurveys()
          this.fetchData()
          this.$message({
            type: 'success',
            message: '移除成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消移除'
        })
      })
    }
  }
}
</script>
